package com.app.megaprosoft.modyimcarcare;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import com.megaprosoft.modyimcarcare.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class InitialServices extends Activity {
	
	connect con = new connect(); 
	ResultSet rs,rs3,rs10;
	SimpleAdapter adapter;  
	int chackTypr; 
	EditText regis,tel,timeHH,timeMM,carpet,insulator;
     String OID,TypeID,RegisterIDS;
	ResultSet rs2;
	String id,ColorCode,Modified,ColorOID;
	DigitalClock digitalCk;
	TextView btn , btn123,spnSize ,CarGroupText;
	
	  public static final int REQUEST_CODE = 1; 
	String ServiceIDS,ServiceType,BrandS,GenerationS,GroupName,
	       ProvinceS,ColorS,SizeS,carGroupS,PhoneS,HHtimeS,MMtimeS,CarpetS,InsulatorS;	
	 
	String hhour,mminute,NameInput,company,RegisterMem,BrandID,CarSize,GroupType,BrandsCheck;
	
	AlertDialog.Builder dDialog1;
	
	public String ans,nws,mws,option;
	 Spinner Brand;
	ArrayList<String> strColor = new ArrayList<String>();
	ArrayList<String> province = new ArrayList<String>();
	ArrayList<String> size = new ArrayList<String>();
	ArrayList<String> sizeID = new ArrayList<String>();
	ArrayList<String> spnBrand = new ArrayList<String>();
	ArrayList<String> SpnGenerations = new ArrayList<String>();
	ArrayList<String> Group = new ArrayList<String>();
	ArrayList<String> hh1 = new ArrayList<String>();
	ArrayList<String> mm1 = new ArrayList<String>();
	
	ArrayList<String> hh2 = new ArrayList<String>();
	ArrayList<String> mm2 = new ArrayList<String>();
	
	RadioButton radioWait,radioNoWait;
	RadioGroup radio;
	
	String timeComeBackHH,timeComeBackMM,timeComeBackAdd;
	
	 Spinner backtimeMM;
	Spinner backtimeHH;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.activity_initial_services);
		
		NameInput = getIntent().getExtras().getString("name");
		company = getIntent().getExtras().getString("CompanyS");
		
		final TextView nameInput = (TextView) findViewById(R.id.nameInput);
		final TextView CompanyInput = (TextView) findViewById(R.id.Company);
		final Button btMem = (Button) findViewById(R.id.btMember);
		final Button btGenPlus = (Button) findViewById(R.id.btGenPlus);
		final Button btClrPlus = (Button) findViewById(R.id.btColorPlus);
		
		dDialog1 = new AlertDialog.Builder(this);
	
		 spnSize = (TextView) findViewById(R.id.spnSize);
		 CarGroupText = (TextView) findViewById(R.id.CarGroup);
		// TextView Band = (TextView) findViewById(R.id.bandShow);
         
        // Band.setText(BrandS);
		 
		 backtimeMM = (Spinner) findViewById(R.id.spnTimem);
		 backtimeHH = (Spinner) findViewById(R.id.spnTimeh);
		 backtimeMM.setEnabled(false);
		 backtimeHH.setEnabled(false);
		 
	
		  btn123 = (TextView) findViewById(R.id.gen);
		  
		 Brand = (Spinner) findViewById(R.id.Brand);
		
		nameInput.setText(NameInput);
		CompanyInput.setText(company); 
   
		//digitalCk = (DigitalClock) findViewById(R.id.digitalClock1);
		
		con = new connect(); 
		
		final Button btH = (Button) findViewById(R.id.BTHole);
		final Button btn = (Button) findViewById(R.id.BTNext);
		final ImageView btHome = (ImageView) findViewById(R.id.BTHome);
		
		regis = (EditText)findViewById(R.id.regis);
		tel = (EditText)findViewById(R.id.tel);

		carpet = (EditText)findViewById(R.id.carpet);
		insulator = (EditText)findViewById(R.id.insulator);
		
	////////////////////////////////////////////////////////////////	
						
						Brand();
						SpnColor();
						province(); 
						//GenerationPlus();
						timeHour();
						timeMinute();
						
						waitForComeBack();
						
	///////////////////////////////////////////////////////////////	
		
		
						Calendar c = Calendar.getInstance();
						SimpleDateFormat df = new SimpleDateFormat("HHmmss");
						String formattedDate = df.format(c.getTime());
		
		//a = ;
		
		
		try {
			id = generateBillNo()+formattedDate;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



		
		try {
			ColorCode = ColorCode();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			ColorOID = ColorOID();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		



		
		
		//////////////////////////////////////////////Mem Insert
		btMem.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
		// TODO Auto-generated method stub
			
		RegisterMem = regis.getText().toString();
		
		if(RegisterMem.equals("")||RegisterMem.equals(null)){
			
			Toast.makeText(InitialServices.this, "��س�����Ţ����¹", Toast.LENGTH_LONG).show();
			
			
		}else{
		
		
		Intent goPlus = new Intent (InitialServices.this,Member.class);
		goPlus.putExtra("RegisterID",RegisterMem);
		goPlus.putExtra("name", NameInput);
		goPlus.putExtra("CompanyS", company);
		startActivity(goPlus);
		}
		
		}
		
		
		
		});
			
		//////////////////////////////////////////////  gen Insert
		btGenPlus.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Toast.makeText(InitialServices.this,BrandsCheck ,Toast.LENGTH_LONG).show(); 
						Intent goPlus = new Intent (InitialServices.this,Generations.class);
						goPlus.putExtra("gen", BrandS);
						goPlus.putExtra("brandID", BrandID);
						goPlus.putExtra("BrandsCheck", BrandsCheck);
						 	
						//startActivity(goPlus);
						startActivityForResult(goPlus, REQUEST_CODE); 
					
					}
						});
		//////////////////////////////////////////////color Insert
		
		final AlertDialog.Builder dDialog = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);
        
		btClrPlus.setOnClickListener(new View.OnClickListener() {
			
			
	////////////////// input color //////////////////////////	
		@Override
		public void onClick(View v) {
		// TODO Auto-generated method stub
		
			

	    	dDialog.setTitle("������! ");
	    	dDialog.setMessage("��س��������� :");
	    	dDialog.setIcon(android.R.drawable.btn_star_big_on); 
	    	dDialog.setView(input);
	    	
	    	// Ok
	    	dDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
	    		public void onClick(DialogInterface dialog, int whichButton) {
	    		  String value = input.getText().toString();
	 			 	
	 			 	
	 			 	 String sql = "INSERT INTO Colors " +
								"(OID,LastModified,ModifiedBy,CreatedDate,CreatedBy,IsActive,IsDeleted,ColorCode,ColorName) " +
								"VALUES (" +
								"'"+ColorOID+"'" + ',' + 
								"GETDATE()" + ',' + 									
								"'100201301220001'" + ',' +
								"GETDATE()" + ',' + 
								"'100201301220001'" + ',' +
								"'"+1+"'" + ',' +
								"'"+0+"'" + ',' +
								"'"+ColorCode+"'" + ',' +
								"'"+ value+"' ) ";
	 			 	 
	 			 	 if(con.addData(sql)){
	 			 		 	Toast.makeText(InitialServices.this,"������ " + value+"����" ,Toast.LENGTH_LONG).show();  	
	 			 		 	strColor.clear();
	 			 		 	SpnColor();
						}else{
							Toast.makeText(InitialServices.this,"�� " + value+"����������" ,Toast.LENGTH_LONG).show();  
					   
						}
	 			 	
	    		  }
	    		});
	    	
	    	// Cancel
	    	dDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	    		  public void onClick(DialogInterface dialog, int whichButton) {
	       			 	Toast.makeText(InitialServices.this,"¡��ԡ��� ������ " ,
	       		 		     Toast.LENGTH_LONG).show();  
	    		  }
	    		});
	    	
	    	dDialog.show();
		}
		});
				
		
		
		
		
		
		// Button go to wash
		
		
		btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				
				
				
				rs2 = con.selectData("SELECT * FROM ServiceType WHERE TypeID = '1' ");
				try {
					rs2.next();
					TypeID = rs2.getString("TypeID");
					ServiceType = rs2.getString("TypeName");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				RegisterIDS = regis.getText().toString();
				PhoneS = tel.getText().toString();
				CarpetS = carpet.getText().toString();
				InsulatorS = insulator.getText().toString();
				
				
				if(PhoneS.equals(null)||PhoneS.equals("")){
	            	
	            	dDialog1.setTitle("�ѧ������͡�������վ�� ");
	            	dDialog1.setIcon(android.R.drawable.btn_star_big_on); 
	            	dDialog1.setMessage("��سҡ�͡�������Ѿ�� ��������ͧ������  '-'");
	            	dDialog1.setPositiveButton("�Դ", null);
	            	dDialog1.show();
	       

	}else{
				
				 String sql = "INSERT INTO CustomerCar " +
							"(ServiceID,ServiceType,Company,Brand,Generation,RegisterID,Province,Color,Size,CarGroup,Phone,Hour,Minute,Carpet,Insulator,CreatedDate,timeComeBack) " +
							"VALUES (" +
							"'"+id+"'" + ',' + 
							"'"+ServiceType+"'" + ',' + 
							"'"+company+"'" + ',' + 
							"'"+BrandS+"'" + ',' + 
							"'"+GenerationS+"'" + ',' +
							"'"+RegisterIDS+"'" + ',' +
							"'"+ProvinceS+"'" + ',' +
							"'"+ColorS+"'" + ',' +
							"'"+CarSize+"'" + ',' +
							"'"+GroupType+"'" + ',' +
							"'"+PhoneS+"'" + ',' +
							"'"+hhour+"'" + ',' +
							"'"+mminute+"'" + ',' +
							"'"+CarpetS+"'" + ',' +
							"'"+InsulatorS+"'" + ',' +
							" GETDATE() " + ',' +
							"'"+timeComeBackAdd+"'"+" ) ";  
				 
				if(con.addData(sql)){	 
					btn.setBackgroundColor(Color.GREEN);
				Toast.makeText(InitialServices.this, "Inserted data successfully", Toast.LENGTH_SHORT).show();
				Intent newActivityAdd = new Intent(InitialServices.this,services.class);
				newActivityAdd.putExtra("HH", hhour);
				newActivityAdd.putExtra("MM", mminute);
				newActivityAdd.putExtra("name", NameInput);
				newActivityAdd.putExtra("CompanyS", company);
				newActivityAdd.putExtra("OID", OID);
				newActivityAdd.putExtra("TypeID", TypeID);
				newActivityAdd.putExtra("RegisterIDS", RegisterIDS);
				newActivityAdd.putExtra("addSer", "");
				startActivity(newActivityAdd);
				}else{
			    Toast.makeText(InitialServices.this, "Inserted data fail", Toast.LENGTH_SHORT).show();
			    
				}
				
				} 
			}
			}); 
		
		// Button go to hole
		
		
		btH.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				
				rs2 = con.selectData("SELECT * FROM ServiceType WHERE TypeID = '2' ");
				try {
					rs2.next();
					TypeID = rs2.getString("TypeID");
					ServiceType = rs2.getString("TypeName");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				RegisterIDS = regis.getText().toString();
				PhoneS = tel.getText().toString();
				//HHtimeS = timeHH.getText().toString();
				//MMtimeS = timeMM.getText().toString();
				CarpetS = carpet.getText().toString();
				InsulatorS = insulator.getText().toString();
				
				
				if(PhoneS.equals(null)||PhoneS.equals("")){
	            	
	            	dDialog1.setTitle("�ѧ������͡�������վ�� ");
	            	dDialog1.setIcon(android.R.drawable.btn_star_big_on); 
	            	dDialog1.setMessage("��سҡ�͡�������Ѿ�� ��������ͧ������  '-'");
	            	dDialog1.setPositiveButton("�Դ", null);
	            	dDialog1.show();
	       

					}else{
			
				 String sql = "INSERT INTO CustomerCar " +
							"(ServiceID,ServiceType,Company,Brand,Generation,RegisterID,Province,Color,Size,CarGroup,Phone,Hour,Minute,Carpet,Insulator,CreatedDate,timeComeBack) " +
							"VALUES (" +
							"'"+id+"'" + ',' + 
							"'"+ServiceType+"'" + ',' + 
							"'"+company+"'" + ',' + 
							"'"+BrandS+"'" + ',' + 
							"'"+GenerationS+"'" + ',' +
							"'"+RegisterIDS+"'" + ',' +
							"'"+ProvinceS+"'" + ',' +
							"'"+ColorS+"'" + ',' +
							"'"+CarSize+"'" + ',' +
							"'"+GroupType+"'" + ',' +
							"'"+PhoneS+"'" + ',' +
							"'"+hhour+"'" + ',' +
							"'"+mminute+"'" + ',' +
							"'"+CarpetS+"'" + ',' +
							"'"+InsulatorS+"'" + ',' +
							" GETDATE() " + ',' +
							"'"+timeComeBackAdd+"'"+" ) "; 

				if(con.addData(sql)){	 
					btH.setBackgroundColor(Color.GREEN);
				Toast.makeText(InitialServices.this, "Inserted data successfully", Toast.LENGTH_SHORT).show();	
				Intent newActivityAdd = new Intent(InitialServices.this,HoleActivity.class);
				newActivityAdd.putExtra("HH", hhour);
				newActivityAdd.putExtra("MM", mminute);
				newActivityAdd.putExtra("name", NameInput);
				newActivityAdd.putExtra("CompanyS", company);
				newActivityAdd.putExtra("OID", OID);
				newActivityAdd.putExtra("TypeID", TypeID);
				newActivityAdd.putExtra("RegisterIDS", RegisterIDS);
				newActivityAdd.putExtra("addHole","");
				startActivity(newActivityAdd);
				}else{
			    Toast.makeText(InitialServices.this, "Inserted data fail", Toast.LENGTH_SHORT).show();
				}
				
					}
			}
			});

		////////////////////////go home
		btHome.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				Intent home = new Intent(InitialServices.this,MainActivity.class);
				home.putExtra("name", NameInput);
				home.putExtra("CompanyS", company);
				startActivity(home);
				
			}
			});

		
    }

	//================================== Spinner Color ========================================
 private void SpnColor() {
		
		final Spinner spn = (Spinner) findViewById(R.id.spnColor);
        rs = con.selectData("SELECT * FROM Colors ");		   
    
		strColor.add("##### ���͡��  #####");	
			try {
				while(rs.next()){          
					strColor.add(rs.getString("ColorName"));
					}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}      
		
			ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,strColor);
			empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
			spn.setAdapter(empolyeeAdapter);
		
		
			spn.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

	            public void onItemSelected(AdapterView<?> adapterView, 
	          	View view, int i, long l) { 
	          	// TODO Auto-generated method stub
	            	
	            	//carGroup = strSpn.get(i).get("expCard"); 
	            	ColorS = strColor.get(i);
	            	
	            	}
	            public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
    				Toast.makeText(InitialServices.this,
             				String.valueOf("Your Selected Empty"),
             					Toast.LENGTH_SHORT).show();
				} 
      });   
  }	
  
//================================== Spinner Province ========================================
 private void province() {
	final Spinner spn = (Spinner) findViewById(R.id.spn);
	
			
			province.add("##### ���͡�ѧ��Ѵ #####");
			province.add("��§����");
			province.add("��§���");
			province.add("�ӻҧ");
			province.add("��ا෾��ҹ��");
			province.add("�Ӿٹ");
			province.add("���");
			province.add("�����");
			province.add("��к��");
			province.add("�ҭ������");
			province.add("����Թ���");
			province.add("��ᾧྪ�");
			province.add("�͹��");		
			province.add("�ѹ�����");
			province.add("���ԧ���");
			province.add("�ź���");
			province.add("��¹ҷ");
			province.add("�������");
			province.add("�����");
			province.add("��ѧ");
			province.add("��Ҵ");
			province.add("�ҡ");
			province.add("��ù�¡");
			province.add("��û��");
			province.add("��þ��");
			province.add("����Ҫ����");
			province.add("�����ո����Ҫ");
			province.add("������ä�");		
			province.add("�������");
			province.add("��Ҹ����");
			province.add("��ҹ");
			province.add("�֧���");
			province.add("���������");
			province.add("�����ҹ�");
			province.add("��ШǺ���բѹ��");
			province.add("��Ҩչ����");
			province.add("�ѵ�ҹ�");
			province.add("��й�������ظ��");
			province.add("�ѧ��");
			province.add("�ѷ�ا");
			province.add("�ԨԵ�");
			province.add("��ɳ��š");
			province.add("ྪú���");
			province.add("ྪú�ó�");
			province.add("����");
			province.add("�����ä��");
			province.add("�ء�����");		
			province.add("�����ͧ�͹");
			province.add("����");
			province.add("��ʸ�");
			province.add("�������");
			province.add("�йͧ");
			province.add("���ͧ");
			province.add("�Ҫ����");
			province.add("ž����");
			province.add("���");
			province.add("�������");
			province.add("ʡŹ��");
			province.add("ʧ���");
			province.add("ʵ��");
			province.add("��طû�ҡ��");
			province.add("��ط�ʧ����");
			province.add("��ط��Ҥ�");
			province.add("������");
			province.add("��к���");
			province.add("�ԧ�����");		
			province.add("��⢷��");
			province.add("�ؾ�ó����");
			province.add("����ɮ��ҹ�");
			province.add("���Թ���");
			province.add("˹ͧ���");
			province.add("˹ͧ�������");
			province.add("��ҧ�ͧ");
			province.add("�شøҹ�");
			province.add("�ط�¸ҹ�");
			province.add("�صôԵ��");
			province.add("�غ��Ҫ�ҹ�");
			province.add("�ӹҨ��ԭ");
			
			
	ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,province);
	empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
	spn.setAdapter(empolyeeAdapter);
	
	spn.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

        public void onItemSelected(AdapterView<?> adapterView, 
      	View view, int i, long l) { 
      	// TODO Auto-generated method stub
        	
        	//carGroup = strSpn.get(i).get("expCard"); 
        	ProvinceS = province.get(i);
        	
        	}
        public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(InitialServices.this,
     				String.valueOf("Your Selected Empty"),
     					Toast.LENGTH_SHORT).show();
		} 

    });
	
	}	

//================================== Spinner Brand ===========================================
  public void Brand() {
	
	

	
	
	
	  rs = con.selectData("SELECT * FROM CarBands ORDER BY "+
				"CASE WHEN [OrderNo] IS NULL THEN (SELECT MAX([OrderNo]) FROM  [dbo].[CarBands]) ELSE [OrderNo] END ASC,"+
				"[CarBandName] ASC");		   

	 spnBrand.add("##### ���͡ ������  #####");	
	 
	
	 
			try {
				while(rs.next()){          
					spnBrand.add(rs.getString("CarBandName"));
					
					
					
					}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}      
		
			
		
			
			
			ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spnBrand);
			empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
			Brand.setAdapter(empolyeeAdapter);
			
			Brand.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

	            public void onItemSelected(AdapterView<?> adapterView, 
	          	View view, int i, long l) { 
	          	// TODO Auto-generated method stub
	            	
	            	//carGroup = strSpn.get(i).get("expCard"); 
	            	BrandS = spnBrand.get(i);
	            	BrandsCheck = spnBrand.get(i); 
	            	
	            	spnSize.setText("-");
	    			CarGroupText.setText("-");
	          
	            	//GenerationSol(BrandS);
	            	//if(BrandS.equals(BrandS)){
	            		
	            		
	            	 rs10 = con.selectData("SELECT * FROM CarBands WHERE CarBandName ='"+BrandS+"' ");		   

	            		// SpnGenerations.add("##### ���͡���  #####");
	            		
	            				try {
	            					if(rs10.next()){
	            						
	            					BrandID = rs10.getString("OID");
	            						
	            					
	            					}
	            					
	            					Toast.makeText(InitialServices.this,BrandID,
	                     					Toast.LENGTH_SHORT).show();
	            				} catch (SQLException e) {
	            					// TODO Auto-generated catch block
	            					e.printStackTrace();
	            				} 
	            				
	            				GenerationSol(BrandID);	
	    
	          	
	            	}
	            public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
    				Toast.makeText(InitialServices.this,
             				String.valueOf("Your Selected Empty"),
             					Toast.LENGTH_SHORT).show();
				}
	            
      });
		
		
		
	}	

//================================== Spinner Generations =====================================


	public void GenerationSol(String option){
	
	final Spinner Generation = (Spinner) findViewById(R.id.Generations);
	
	SpnGenerations.clear();
	
	
	 rs = con.selectData("SELECT * FROM CarGenerations WHERE CarBandOID ='"+option+"' ");		   

	// SpnGenerations.add("##### ���͡���  #####");
	
			try {
				while(rs.next()){ 
					
					SpnGenerations.add(rs.getString("CarGenerationName"));
					
					
					}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}      
		
			ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,SpnGenerations);
			empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
			Generation.setAdapter(empolyeeAdapter);
			
			Generation.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

	            public void onItemSelected(AdapterView<?> adapterView, 
	          	View view, int i, long l) { 
	          	// TODO Auto-generated method stub
	            	
	            	//carGroup = strSpn.get(i).get("expCard"); 
	            	GenerationS = SpnGenerations.get(i);
	            	
	            	
	            
	            	 rs10 = con.selectData("SELECT * FROM CarGenerations WHERE CarGenerationName ='"+GenerationS+"' ");		   

	            		// SpnGenerations.add("##### ���͡���  #####");
	            	 
	            		
	            				try {
	            					if(rs10.next()){
	            						
	            						CarSize = rs10.getString("CarSize");
	            						GroupType = rs10.getString("CarGroupOID");
	            						
	            					}
	            					
	            					Toast.makeText(InitialServices.this,CarSize,
	                     					Toast.LENGTH_SHORT).show();
	            					
	            					Toast.makeText(InitialServices.this,GroupType,
	                     					Toast.LENGTH_SHORT).show();
	            				} catch (SQLException e) {
	            					// TODO Auto-generated catch block
	            					e.printStackTrace();
	            				} 
	
	  //================================== Spinner Size ========================================	
	            				spnSize.setText(CarSize);
	            				CarGroupName(GroupType);
	            	 
	            	}
	            public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
    				Toast.makeText(InitialServices.this,
             				String.valueOf("Your Selected Empty"),
             					Toast.LENGTH_SHORT).show();
				} 

      });
		
	}	

//================================== Spinner CarGroup ========================================
private void CarGroupName(String groupID) {
	
	Toast.makeText(InitialServices.this,groupID,
				Toast.LENGTH_SHORT).show();


	
	 rs = con.selectData("SELECT * FROM CarGroup WHERE OID = '"+groupID+"'");		   
      
	
			try {
				if(rs.next()){          
		
					GroupName = rs.getString("CarGroupName");
	            	OID = rs.getString("OID");
					}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}      
			
			Toast.makeText(InitialServices.this,GroupName,
					Toast.LENGTH_SHORT).show();
		
			CarGroupText.setText(GroupName);
	            
	            	
        	
      }
			
	



public String generateBillNo() throws Exception{

    
    String StrSql = "SELECT COUNT(ServiceID)+1," +
    		  "MONTH(GETDATE()), "+
   		  "CONVERT(VARCHAR(2), GETDATE(), 11) "+
            "FROM CustomerCar "+
            "WHERE CONVERT(VARCHAR(7),[CreatedDate], 111) = CONVERT(VARCHAR(7), GETDATE(), 111) ";


			rs = con.selectData(StrSql);
			
			String No = "";
				
			if(rs.next()){
				if(rs.getInt(1) < 10)
					No = "00"+rs.getInt(1);
				else if(rs.getInt(1) < 100)
					No = "0"+rs.getInt(1);
				else
					No = ""+rs.getInt(1);
			
		
				return ""+rs.getInt(3)+""+rs.getInt(2)+No;	// Prefix + YYMMDD + XXXX
				
			}else{
				
				return null;
				//return null;
			}
}

private void timeHour() {
	
	final Spinner timeHH = (Spinner) findViewById(R.id.spnHour);
	
			hh1.add("00");
			hh1.add("01");
			hh1.add("02");
			hh1.add("03");
			hh1.add("04");
			hh1.add("05");
			hh1.add("06");		
			hh1.add("07");
			hh1.add("08");
			hh1.add("09");
			hh1.add("10");
			hh1.add("11");
			hh1.add("12");
			hh1.add("13");
			hh1.add("14");
			hh1.add("15");
			hh1.add("16");
			hh1.add("17");
			hh1.add("18");
			hh1.add("19");
			hh1.add("20");
			hh1.add("21");
			hh1.add("22");
			hh1.add("23");		
			
			
	ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>

(this,android.R.layout.simple_spinner_item,hh1);
	empolyeeAdapter.setDropDownViewResource

(android.R.layout.simple_dropdown_item_1line);
	timeHH.setAdapter(empolyeeAdapter);
	
	timeHH.setOnItemSelectedListener(new  

AdapterView.OnItemSelectedListener() { 

        public void onItemSelected(AdapterView<?> adapterView, 
      	View view, int i, long l) { 
      	// TODO Auto-generated method stub
        	
        	//carGroup = strSpn.get(i).get("expCard"); 
        	hhour = hh1.get(i);
        	
        	}
        public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(InitialServices.this,
     				String.valueOf("Your Selected Empty"),
     					

Toast.LENGTH_SHORT).show();
		} 

    });
	
	}	
////////////////////////////////////////////////////////////////////////////////////////////
private void timeMinute() {
	final Spinner timeMM = (Spinner) findViewById(R.id.spnMinute);
		
			mm1.add("00");
			mm1.add("01");
			mm1.add("02");
			mm1.add("03");
			mm1.add("04");
			mm1.add("05");
			mm1.add("06");		
			mm1.add("07");
			mm1.add("08");
			mm1.add("09");
			mm1.add("10");
			mm1.add("11");
			mm1.add("12");
			mm1.add("13");
			mm1.add("14");
			mm1.add("15");
			mm1.add("16");
			mm1.add("17");
			mm1.add("18");
			mm1.add("19");
			mm1.add("20");
			mm1.add("21");
			mm1.add("22");
			mm1.add("23");		
			mm1.add("24");
			mm1.add("25");
			mm1.add("26");
			mm1.add("27");
			mm1.add("28");
			mm1.add("29");
			mm1.add("30");
			mm1.add("31");
			mm1.add("32");
			mm1.add("33");
			mm1.add("34");
			mm1.add("35");
			mm1.add("36");
			mm1.add("37");
			mm1.add("38");
			mm1.add("39");
			mm1.add("40");
			mm1.add("41");
			mm1.add("42");
			mm1.add("43");
			mm1.add("44");		
			mm1.add("45");
			mm1.add("46");
			mm1.add("47");
			mm1.add("48");
			mm1.add("49");
			mm1.add("50");
			mm1.add("51");
			mm1.add("52");
			mm1.add("53");
			mm1.add("54");
			mm1.add("55");
			mm1.add("56");
			mm1.add("57");
			mm1.add("58");
			mm1.add("59");
			
			
	ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>

(this,android.R.layout.simple_spinner_item,mm1);
	empolyeeAdapter.setDropDownViewResource

(android.R.layout.simple_dropdown_item_1line);
	timeMM.setAdapter(empolyeeAdapter);
	
	timeMM.setOnItemSelectedListener(new  

AdapterView.OnItemSelectedListener() { 

        public void onItemSelected(AdapterView<?> adapterView, 
      	View view, int i, long l) { 
      	// TODO Auto-generated method stub
        	
        	//carGroup = strSpn.get(i).get("expCard"); 
        	mminute = mm1.get(i);
        	
        	}
        public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(InitialServices.this,
     				String.valueOf("Your Selected Empty"),
     					

Toast.LENGTH_SHORT).show();
		} 

    });
	
	}

public String ColorCode() throws Exception{

    
    String StrSql = "SELECT COUNT(ColorCode)+1 FROM Colors  ";

			rs = con.selectData(StrSql);
			
			String No = "";
				
			if(rs.next()){
				
					No = ""+rs.getInt(1);
				
		
				return No;	// Prefix + YYMMDD + XXXX
				
			}else{
				
				return null;
				//return null;
			}
}

public String ColorOID() throws Exception{

  
    String StrSql = "SELECT COUNT(OID)+1," +
    		  "DAY(GETDATE()), "+
    		  "MONTH(GETDATE()), "+
    		  "CONVERT(VARCHAR(2)," +
    		  "GETDATE(), 11) "+
            "FROM Colors "+
            "WHERE CONVERT(VARCHAR,[CreatedDate], 111) = CONVERT(VARCHAR, GETDATE(), 111) ";
    
			rs = con.selectData(StrSql);
			
			String No = "";
			String day = "";
				
			if(rs.next()){
				if(rs.getInt(1) < 10){
					No = "000"+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
				}
				else if(rs.getInt(1) < 100){
					No = "00"+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
					
				}
				else if(rs.getInt(1) < 1000){
					No = "0"+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
					
				}
				else{
					No = ""+rs.getInt(1);
					if(rs.getInt(2) < 10)
						day = "0"+rs.getInt(2);
					
					
				}
			
		
				return "13020"+rs.getInt(4)+rs.getInt(3)+""+day+""+No;	// Prefix + YYMMDD + XXXX
				
				
				
				
			}else{
				
				return null;
				//return null;
			}
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	// TODO Auto-generated method stub
	super.onActivityResult(requestCode, resultCode, data);
	Log.i("Get data", requestCode+"");

	switch (requestCode) {
	case services.REQUEST_CODE:
		
		spnBrand.clear();
		 	Brand();
		 	
		 String	brendCheck = data.getStringExtra("BrandsCheck");
		for(int i = 0;i<spnBrand.size();i++) {
		 if(brendCheck.equals(spnBrand.get(i))){
			 Brand.setSelection(i);
		 }
		 else{
				Toast.makeText(InitialServices.this,
	     				String.valueOf("���������������"),
	     					

	Toast.LENGTH_SHORT).show();
		 }
		}
		break;

	default:
		break;
	}	
	
}



private void backtimeHour() {
	
	hh2.clear();
			hh2.add("00");
			hh2.add("01");
			hh2.add("02");
			hh2.add("03");
			hh2.add("04");
			hh2.add("05");
			hh2.add("06");		
			hh2.add("07");
			hh2.add("08");
			hh2.add("09");
			hh2.add("10");
			hh2.add("11");
			hh2.add("12");
			hh2.add("13");
			hh2.add("14");
			hh2.add("15");
			hh2.add("16");
			hh2.add("17");
			hh2.add("18");
			hh2.add("19");
			hh2.add("20");
			hh2.add("21");
			hh2.add("22");
			hh2.add("23");		
			
			ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,hh2);
			empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
			backtimeHH.setAdapter(empolyeeAdapter);
			
			backtimeHH.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

		        public void onItemSelected(AdapterView<?> adapterView, 
		      	View view, int i, long l) { 
		      	// TODO Auto-generated method stub
		        	timeComeBackHH = hh2.get(i);
		        	timeComeBackAdd = timeComeBackHH + ":" + timeComeBackMM;
		      	
		        	}
		        public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					Toast.makeText(InitialServices.this,
		     				String.valueOf("Your Selected Empty"),
		     					Toast.LENGTH_SHORT).show();
				}
		        
		});
	
	}	



private void backtimeMinute() {
	
	
	
	mm2.clear();
			mm2.add("00");
			mm2.add("01");
			mm2.add("02");
			mm2.add("03");
			mm2.add("04");
			mm2.add("05");
			mm2.add("06");		
			mm2.add("07");
			mm2.add("08");
			mm2.add("09");
			mm2.add("10");
			mm2.add("11");
			mm2.add("12");
			mm2.add("13");
			mm2.add("14");
			mm2.add("15");
			mm2.add("16");
			mm2.add("17");
			mm2.add("18");
			mm2.add("19");
			mm2.add("20");
			mm2.add("21");
			mm2.add("22");
			mm2.add("23");		
			mm2.add("24");
			mm2.add("25");
			mm2.add("26");
			mm2.add("27");
			mm2.add("28");
			mm2.add("29");
			mm2.add("30");
			mm2.add("31");
			mm2.add("32");
			mm2.add("33");
			mm2.add("34");
			mm2.add("35");
			mm2.add("36");
			mm2.add("37");
			mm2.add("38");
			mm2.add("39");
			mm2.add("40");
			mm2.add("41");
			mm2.add("42");
			mm2.add("43");
			mm2.add("44");		
			mm2.add("45");
			mm2.add("46");
			mm2.add("47");
			mm2.add("48");
			mm2.add("49");
			mm2.add("50");
			mm2.add("51");
			mm2.add("52");
			mm2.add("53");
			mm2.add("54");
			mm2.add("55");
			mm2.add("56");
			mm2.add("57");
			mm2.add("58");
			mm2.add("59");
			
		

	ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mm2);
	empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
	backtimeMM.setAdapter(empolyeeAdapter);
	
	backtimeMM.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener() { 

        public void onItemSelected(AdapterView<?> adapterView, 
      	View view, int i, long l) { 
      	// TODO Auto-generated method stub
        	timeComeBackMM = mm2.get(i);

        	timeComeBackAdd = timeComeBackHH + ":" + timeComeBackMM;
        	}
        public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			Toast.makeText(InitialServices.this,
     				String.valueOf("Your Selected Empty"),
     					Toast.LENGTH_SHORT).show();
		}
        
});



}	


public void waitForComeBack(){
	
	 radio= (RadioGroup)findViewById(R.id.wanttowait);
	 radioWait = (RadioButton)findViewById (R.id.wait);
	 radioNoWait = (RadioButton)findViewById (R.id.nowait);
	 


	 radio.setOnCheckedChangeListener ( new RadioGroup.OnCheckedChangeListener ( )
	 {
	 public void onCheckedChanged ( RadioGroup group, int checkedId )
	 {
	 RadioButton checkedRadio = (RadioButton) InitialServices.this.findViewById (checkedId);
	 chackTypr = checkedRadio.getId(); 
	 
	   if(chackTypr == R.id.wait){
		   
		   backtimeHour();
		   backtimeMinute();
		   backtimeMM.setEnabled(true);
		   backtimeHH.setEnabled(true);
  
	
	   } 
	   else if (chackTypr == R.id.nowait) {
		   
		   
		     backtimeMM = (Spinner) findViewById(R.id.spnTimem);
			 backtimeHH = (Spinner) findViewById(R.id.spnTimeh);
			 ArrayList<String> hh2 = new ArrayList<String>();
			ArrayList<String> mm2 = new ArrayList<String>();
			 
			ArrayAdapter<String> empolyeeAdapter = new ArrayAdapter<String>(InitialServices.this,android.R.layout.simple_spinner_item,hh2);
			empolyeeAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
			backtimeHH.setAdapter(empolyeeAdapter);
			
			ArrayAdapter<String> empolyeeAdapter2 = new ArrayAdapter<String>(InitialServices.this,android.R.layout.simple_spinner_item,mm2);
			empolyeeAdapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
			backtimeMM.setAdapter(empolyeeAdapter2);
		   
		   backtimeMM.setSelected(false);
		   backtimeHH.setSelected(false);
		   backtimeMM.setEnabled(false);
		   backtimeHH.setEnabled(false);
		   timeComeBackAdd();
		  
	        } 

	   }
	 });
	
}

public void timeComeBackAdd() {
	
	timeComeBackAdd = "-";
	
	  
}




}










